package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClassification;
import com.ruoyi.system.service.ISysClassificationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品分类Controller
 * 
 * @author jiashu
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/system/classify")
public class SysClassificationController extends BaseController
{
    @Autowired
    private ISysClassificationService sysClassificationService;

    /**
     * 查询药品分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify:list')")
    @GetMapping("/list")
    public List<SysClassification> list(SysClassification sysClassification)
    {
//        startPage();
        List<SysClassification> list = sysClassificationService.selectSysClassificationList(sysClassification);
//        return getDataTable(list);
        return list;
    }

    /**
     * 导出药品分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify:export')")
    @Log(title = "药品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysClassification sysClassification)
    {
        List<SysClassification> list = sysClassificationService.selectSysClassificationList(sysClassification);
        ExcelUtil<SysClassification> util = new ExcelUtil<SysClassification>(SysClassification.class);
        util.exportExcel(response, list, "药品分类数据");
    }

    /**
     * 获取药品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classify:query')")
    @GetMapping(value = "/{classificationId}")
    public AjaxResult getInfo(@PathVariable("classificationId") Long classificationId)
    {
        return success(sysClassificationService.selectSysClassificationByClassificationId(classificationId));
    }


    /**
     * 获取全部分类树
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:classify:query')")
    @GetMapping(value = "/getRoot")
    public AjaxResult getRoot()
    {
        return success(sysClassificationService.selectRootClassification());
    }

    /**
     * 根据某一父节点ID获取全部子节点
     * @param classificationId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:classify:query')")
    @GetMapping(value = "/getClassById/{classificationId}")
    public AjaxResult getClassById(@PathVariable("classificationId") Long classificationId)
    {
        return success(sysClassificationService.selectChildrenByParentId(classificationId));
    }



    /**
     * 新增药品分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:add')")
    @Log(title = "药品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysClassification sysClassification)
    {
        String classificationName = sysClassification.getClassificationName();
        SysClassification sysClassification1 = new SysClassification();
        sysClassification1.setClassificationName(classificationName);
        List<SysClassification> sysClassifications = sysClassificationService.selectSysClassificationList(sysClassification1);
        if(!sysClassifications.isEmpty()){
            return AjaxResult.error("该分类名称已存在");
        }


        sysClassification.setCreateBy(getUsername());
        sysClassification.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysClassificationService.insertSysClassification(sysClassification));
    }

    /**
     * 修改药品分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:edit')")
    @Log(title = "药品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysClassification sysClassification)
    {
        // 检查修改后的名称是否可用
        if (!sysClassificationService.checkClassificationName(sysClassification)) {
            return AjaxResult.error("分类名称重复");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysClassification.setCreateBy(loginUser.getUsername());
        sysClassification.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysClassificationService.updateSysClassification(sysClassification));
    }

    /**
     * 删除药品分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:remove')")
    @Log(title = "药品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classificationIds}")
    public AjaxResult remove(@PathVariable Long[] classificationIds)
    {
        // 如果删除的分类下方还有子项则不允许删除
        for (Long classificationId : classificationIds) {
            List<SysClassification> sysClassifications = sysClassificationService.selectChildrenByParentId(classificationId);
            if(!sysClassifications.isEmpty()){
                return error("该分类点下还有未删除的子分类");
            }
        }

        return toAjax(sysClassificationService.deleteSysClassificationByClassificationIds(classificationIds));
    }
}
